package com.qu.waibao.my.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.qu.waibao.AppConfig;
import com.qu.waibao.R;
import com.qu.waibao.base.BaseActivity;
import com.qu.waibao.base.BaseRecyclerAdapter;
import com.qu.waibao.bean.Jifen;
import com.qu.waibao.home.activity.ChouActivity;
import com.qu.waibao.home.activity.WebActivity;
import com.qu.waibao.interf.XnetCallBack;
import com.qu.waibao.my.adapter.JifenAdapter;
import com.qu.waibao.network.ActionUtil;
import com.qu.waibao.network.NetManager;
import com.qu.waibao.utils.GsonUtil;
import com.qu.waibao.widget.RecyclerRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class JifenActivity extends BaseActivity implements View.OnClickListener,
        RecyclerRefreshLayout.SuperRefreshLayoutListener{
    @BindView(R.id.tv_title)
    TextView tv_title;
    @BindView(R.id.iv_publish)
    ImageView iv_publish;
    @BindView(R.id.mRecyclerRefreshLayout)
    RecyclerRefreshLayout mRecyclerRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_jifen)
    TextView tv_jifen;
    private JifenAdapter adapter;
    private int pageNum = 1;
    private boolean isRefreshing = true;
    @Override
    protected int getContentView() {
        return R.layout.activity_jifen;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        tv_title.setText("我的积分");
        iv_publish.setVisibility(View.GONE);
        mRecyclerRefreshLayout.setColorSchemeResources(R.color.colorPrimaryDark);
        mRecyclerRefreshLayout.setSuperRefreshLayoutListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new JifenAdapter(this, BaseRecyclerAdapter.ONLY_FOOTER);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);
        if(adapter.getItems().size() == 0){
            mRecyclerRefreshLayout.setRefreshing(true);
            onRefreshing();
        }
    }

    @OnClick({R.id.ll_back,R.id.tv_qian,R.id.tv_chou})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ll_back:
                finish();
                break;
            case R.id.tv_qian:
                Intent intent = new Intent(this, WebActivity.class);
                intent.putExtra("title","签到");
                startActivity(intent);
                break;
            case R.id.tv_chou:
                Intent intents = new Intent(this, ChouActivity.class);
                intents.putExtra("title","抽奖");
                startActivity(intents);
                break;
        }
    }

    public void requestData(){
        Map<String,String> map = new HashMap<>();
        map.put("action", ActionUtil.jf_list_get);
        map.put("userid", AppConfig.getString("userid"));
        map.put("pagenum",pageNum+"");
        NetManager.getInstance().request(this, map, false, new XnetCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                Jifen jifen = GsonUtil.createGson().fromJson(result,Jifen.class);
                tv_jifen.setText(jifen.getJf_total());
                if(isRefreshing){
                    adapter.clear();
                    adapter.addAll(jifen.getJf_detaillist());
                    if(jifen.getJf_detaillist().size() < 12){
                        adapter.setState(BaseRecyclerAdapter.STATE_NO_MORE,true);
                    }
                }else {
                    adapter.addAll(jifen.getJf_detaillist());
                    if(jifen.getJf_detaillist().size() < 12){
                        adapter.setState(BaseRecyclerAdapter.STATE_NO_MORE,true);
                    }
                    if(jifen.getJf_detaillist().size() == 0){
                        pageNum--;
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if(pageNum > 1){
                    pageNum--;
                    onComplate();
                }
            }

            @Override
            public void onFinished() {
               onComplate();
            }
        });
    }

    @Override
    public void onRefreshing() {
        isRefreshing  = true;
        pageNum = 1;
        requestData();
    }

    @Override
    public void onLoadMore() {
        isRefreshing  = false;
        pageNum++;
        adapter.setState(BaseRecyclerAdapter.STATE_LOAD_MORE,true);
        requestData();
    }
    public void onComplate(){
        mRecyclerRefreshLayout.onComplete();
        isRefreshing = false;
    }
}
